/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxitemmapi.p                                     */
/* PROCEDURE TYPE        : Item Master Load CIM-LOAD Process                */
/* DESCRIPTION           : Item Master Load CIM-LOAD Process                */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                5-Jul-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}


{xxapilog.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vi_reccount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.

define buffer b_psmstr for ps_mstr.

define temp-table ttpsmstr no-undo
           field ps_domain       like ps_mstr.ps_domain   
           field ps_par          like ps_mstr.ps_par     
           field ps_comp         like ps_mstr.ps_comp    
           field ps_ref          like ps_mstr.ps_ref     
           field ps_qty_per      like ps_mstr.ps_qty_per 
           field ps_start        like ps_mstr.ps_start   
           field ps_end          like ps_mstr.ps_end     
           field ps_ps_code      like ps_mstr.ps_ps_code 
           field ps_rmks         like ps_mstr.ps_rmks    
           field ps_scrp_pct     like ps_mstr.ps_scrp_pct
           field ps_lt_off       like ps_mstr.ps_lt_off  
           field ps_op           like ps_mstr.ps_op      
           field ps_item_no      like ps_mstr.ps_item_no 
           field ps_fcst_pct     like ps_mstr.ps_fcst_pct
           field ps_group        like ps_mstr.ps_group   
           field ps_process      like ps_mstr.ps_process 
           field ps_type         as   character
           field ps_traceid       as   character  
           field ps_dataid       as   character  
           field ps_cimfile      as   character  
           field ps_logfile       as   character  

           index parcomp ps_domain ps_par ps_comp
           index ptaskid ps_traceid.

           
   DEFINE DATASET dsBommaster FOR ttpsmstr.

   hdataset = dataset dsBommaster:HANDLE.    
                

  
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "bm/"
       m_archpath    = m_archpath + "bm/"
       m_successpath = m_successpath + "bm/"
       m_errorpath   = m_errorpath + "bm/"
       m_workpath    = m_workpath + "bm/".


   empty temp-table ttpsmstr.
   
   define buffer b_ttpsmstr for ttpsmstr.
   
   v_runproagram = "".
   vc_traceid = "".

   /* Load Bom master data from JSON to temp-table */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttpsmstr no-error.
      assign vc_traceid = ttpsmstr.ps_traceid.
   end.


    /* validation input data */
   
   /* 校验零件号是否存在 */
   /* 校验BOM开始和结束日期是否重叠 */
   /* 校验工序是否存在 */
   /* */
   /*  */
   /*  */


    /* Create CIM-LOAD file and do cim-load */
   v_cim = string(vc_traceid).
   v_log = string(vc_traceid).
   
  output to /apps/eai/work/cim/xxbomapi.log  .      
  
 if vc_import and vl_RetOK then do:  

   /* create cim load format */
                 
   cimloadprocess:
   do transaction on error undo cimloadprocess, leave cimloadprocess: 
   
   for each ttpsmstr  break by ttpsmstr.ps_dataid.

    if first-of(ttpsmstr.ps_dataid) then do:
     
      v_toterrorstatus = no.
      
     v_cim = m_workpath + "BM" + "_" + string(ttpsmstr.ps_traceid) + "_" + string(ttpsmstr.ps_dataid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
     v_log = m_workpath + "BM" + "_" + string(ttpsmstr.ps_traceid) + "_" + string(ttpsmstr.ps_dataid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".

     output stream f_cim to value(v_cim). 

     end.
      
      for each ps_mstr no-lock where ps_mstr.ps_par = ttpsmstr.ps_par 
                                  and ps_mstr.ps_comp =  ttpsmstr.ps_comp
                                  and ps_mstr.ps_ref = ttpsmstr.ps_ref
                                  and ps_mstr.ps_domain = global_domain
                                  break by ps_mstr.ps_par
                                        by ps_mstr.ps_start.
                                  
      if last-of(ps_mstr.ps_start) then 
      do:
         
         /* Effitive date overlap  */
         if ps_mstr.ps_start > ttpsmstr.ps_start and ttpsmstr.ps_type <> "D"then 
         do:
            create api_status.
            assign api_status.data_seq         = 1
                   api_status.traceid          = ttpsmstr.ps_traceid
                   api_status.dataid           = ttpsmstr.ps_dataid
                   api_status.billnbr          = ttpsmstr.ps_par + "|" + ttpsmstr.ps_comp
                   api_status.return_status    = "ERROR"  
                   api_status.message_type     = "Pre-validation"
                   api_status.message_string   = gettermlabel("BOM_DATE_OVERLAP",20) + 
                                                       string(ps_mstr.ps_start) + "-" + 
                                                        string(ttpsmstr.ps_start).                                                        
             assign   v_toterrorstatus = yes.
         end. /* if ps_mstr.ps_start > ttpsmstr.ps_start then  */
         
         /* change end date for last version bom  */
         if ps_mstr.ps_start < ttpsmstr.ps_start and ttpsmstr.ps_type <> "D" then 
         do:
           for each b_psmstr where b_psmstr.ps_par = ps_mstr.ps_par
                               and b_psmstr.ps_domain = global_domain
                               break by b_psmstr.ps_comp
                               by b_psmstr.ps_start
                               .
            if last-of(b_psmstr.ps_start) then 
             do:
            put stream f_cim unformatted  v_quote b_psmstr.ps_par       v_quote   skip                    /* PARENT part */
                                    v_quote b_psmstr.ps_comp      v_quote v_space                         /* Component */
                                    v_quote b_psmstr.ps_ref       v_quote v_space                         /* Reference  */
                                    v_quote b_psmstr.ps_start     v_quote skip   
                                    v_default v_space                                                    /* QuantitPer */
                                    v_default v_space                                                    /* StructureType  */
                                    v_default v_space                                                    /* Start Effitive  */
                                    v_quote ttpsmstr.ps_start - 1 v_quote v_space                        /* End Effitive  */                                  
                                    v_default v_space                                                    /* remarks  */
                                    v_default v_space                                                    /* scrap pct */
                                    v_default v_space                                                    /* lead time  */
                                    v_default v_space                                                    /* operation  */
                                    v_default v_space                                                    /* process  */
                                    skip
                                    "."                                                                  /* F4 to parent screen */
                                    skip .
               end. /* if last-of(b_psmstr.ps_start) then  */
            end. /* for each b_psmstr where b_psmstr.ps_par = ps_mstr.ps_par */
         end. /* else if ps_mstr.ps_start < ttpsmstr.ps_start and ttpsmstr.ps_type <> "D" then  */
         
      end. /*  if last-of(ps_mstr.ps_start) then  */
     
    end. /* for each ps_mstr no-lock where ps_mstr.ps_par = ttpsmstr.ps_par   */
      put stream f_cim unformatted  v_quote ttpsmstr.ps_par       v_quote   skip                          /* PARENT part */
                                    v_quote ttpsmstr.ps_comp      v_quote v_space                         /* Component */
                                    v_quote ttpsmstr.ps_ref       v_quote v_space.                        /* Reference  */
      if ttpsmstr.ps_type = "D" then 
      put stream f_cim unformatted  v_quote ttpsmstr.ps_start v_quote v_space
                                    v_quote "X"  v_quote skip                                             /* Delete */
                                    v_quote "-"  v_quote skip                                          
                                    v_quote "yes" v_quote skip
                                    "."                                                                      /* F4 to parent screen */
                                    skip 
                                    .
                                         
       else        
       put stream f_cim unformatted v_quote ttpsmstr.ps_start     v_quote skip   
                                    v_quote ttpsmstr.ps_qty_per   v_quote v_space                           /* QuantitPer */
                                    v_quote ttpsmstr.ps_ps_code   v_quote v_space                           /* StructureType  */
                                    v_quote ttpsmstr.ps_start     v_quote v_space                           /* Start Effitive  */
                                    v_quote ttpsmstr.ps_end       v_quote v_space                           /* End Effitive  */                                  
                                    v_quote ttpsmstr.ps_rmks      v_quote v_space                           /* remarks  */
                                    v_quote ttpsmstr.ps_scrp_pct  v_quote v_space                           /* scrap pct */
                                    v_quote ttpsmstr.ps_lt_off    v_quote v_space                           /* lead time  */
                                    v_quote ttpsmstr.ps_op        v_quote v_space                           /* operation  */
                                    v_quote ttpsmstr.ps_process   v_quote v_space                           /* process  */
                                    skip
                                    "."                                                                      /* F4 to parent screen */
                                    skip 
                                    .
   
    if last-of(ttpsmstr.ps_dataid) then do:
    output stream f_cim close.
    
   
         
     /* cim load process */
     v_runproagram = "bmpsmt.p".
       
     execname =  "xxbomapi.p".
 
     if v_toterrorstatus = no then 
     do:                
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input vc_traceid,
                          input ttpsmstr.ps_dataid,
                          input ttpsmstr.ps_par + "|" + ttpsmstr.ps_comp,
                          output v_errstring,
                          output v_errorstatus) .
                          
       v_toterrorstatus = v_toterrorstatus or v_errorstatus.    
      end. /* if v_toterrorstatus = no then  */

            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).  


         assign   ttpsmstr.ps_cimfile = replace(v_cim,m_workpath,vc_topath)
                  ttpsmstr.ps_logfile = replace(v_log,m_workpath,vc_topath).
                            
                 
       end. /* do vi_filecount = 1 to 2: */   
     end. /* if last-of */
     
    end. /* for each ttpsmstr  */
     if  v_toterrorstatus then 
                 undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */
       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         


   end. /* if vc_import */
   
        /* Handle cimload related files */
         /* Create output json */  

      for each ttpsmstr  .
      
       vi_reccount = vi_reccount + 1.
              
       run CreateAPILogMaster(input vc_traceid,
                              input "createbommaster",
                              input "xxbomapi.p",
                              input ttpsmstr.ps_cimfile,
                              input ttpsmstr.ps_logfile,
                              input vc_inputjson,
                              input ttpsmstr.ps_par + "|" + ttpsmstr.ps_comp,
                              input "",
                              input "xxapibomdata_det",
                              input vi_reccount).  
                              
       end.









         
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
